package com.leetcode.partition20;

import java.io.*;
import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2022/1/28 16:17
 */
public class LC1996游戏中弱角色的数量 {

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        int n = Integer.parseInt(reader.readLine());
        int[][] properties = new int[n][2];
        for (int i = 0; i < n; i++) {
            String[] s = reader.readLine().split(" ");
            int a = Integer.parseInt(s[0]), b = Integer.parseInt(s[1]);
            properties[i] = new int[]{a, b};
        }
        writer.write(numberOfWeakCharacters(properties) + "\n");
        writer.flush();
    }

    public static int numberOfWeakCharacters(int[][] properties) {
        int n = properties.length, ans = 0;
        //先按照攻击力进行排序，如果攻击力相同，就按照防御力进行排序
        Arrays.sort(properties, 0, n, (o1, o2) -> o1[0] != o2[0] ? Integer.compare(o2[0], o1[0]) : Integer.compare(o2[1], o1[1]));
        //max始终保存之前组的最大防御力
        for (int i = 0, max = properties[0][1]; i < n; ) {
            //将当前所有攻击力相同的角色看做一组
            int j = i;
            while (j < n && properties[i][0] == properties[j][0]) {
                //检查组内是否有弱角色（防御力低于之前组的最高防御力）
                if (i > 0 && properties[j][1] < max) ans++;
                j++;
            }
            max = Math.max(max, properties[i][1]);
            i = j;
        }
        return ans;
    }
}
